package org.linlinjava.litemall.wx.web;

import java.math.BigDecimal;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallFormPurchaseItem;
import org.linlinjava.litemall.db.service.PurchaseService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 药材供应
 */
@RestController
@RequestMapping("/wx/purchase")
@Validated
public class WxPurchaseController {
	private final Log logger = LogFactory.getLog(WxPurchaseController.class);

	@Autowired
	private PurchaseService purchaseService;
	@Autowired
	private WxPurchaseService wxPurchaseService;

	@GetMapping("herbTotalList")
	public Object herbTotalList(@LoginUser Integer userId, @NotNull Byte type,
			@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,
			@Sort @RequestParam(defaultValue = "add_time") String sort,
			@Order @RequestParam(defaultValue = "desc") String order, @RequestParam(defaultValue = "") Integer herbId,
			@RequestParam(defaultValue = "") String searchValue) {

		return wxPurchaseService.herbTotalList(userId, type, page, limit, sort, order, herbId,searchValue);
	}

	@GetMapping("list")
	public Object list(@LoginUser Integer userId, @NotNull Byte type, 
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit,
			@Sort @RequestParam(defaultValue = "add_time") String sort,
			@Order @RequestParam(defaultValue = "desc") String order, 
			@RequestParam(defaultValue = "") String genericName,
			@RequestParam(defaultValue = "") String describe,
			@RequestParam(defaultValue = "") String searchValue) {

		return wxPurchaseService.list(userId, type, page, limit, sort, order, genericName, describe, searchValue);
	}

	@GetMapping("detail")
	public Object detail(@LoginUser Integer userId, @NotNull Integer id, @NotNull Integer itemId) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		return wxPurchaseService.detail(userId, id, itemId);
	}

	/**
	 * 经营范围外列表
	 * @param userId
	 * @param type
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @param genericName
	 * @param describe
	 * @param searchValue
	 * @return
	 */
	@GetMapping("extraList")
	public Object extraList(@LoginUser Integer userId, @NotNull Byte type, 
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit,
			@Sort @RequestParam(defaultValue = "add_time") String sort,
			@Order @RequestParam(defaultValue = "desc") String order, 
			@RequestParam(defaultValue = "") String genericName,
			@RequestParam(defaultValue = "") String describe,
			@RequestParam(defaultValue = "") String searchValue) {

		return wxPurchaseService.extraList(userId, type, page, limit, sort, order, genericName, describe, searchValue);
	}

	@GetMapping("extraDetail")
	public Object extraDetail(@LoginUser Integer userId, @NotNull Integer id, @NotNull Integer itemId) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		return wxPurchaseService.extraDetail(userId, id, itemId);
	}

	@PostMapping("amountUpdate")
	public Object amountUpdate(@LoginUser Integer userId, @RequestBody Map<String, Object> map) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		Integer formPurchaseId = MapUtils.getInteger(map, "id");
		Integer id = MapUtils.getInteger(map, "itemId");
		if (formPurchaseId == null || id == null) {
			return ResponseUtil.badArgument();
		}
		String receivedAmountStr = MapUtils.getString(map, "receivedAmount", "0");
		BigDecimal receivedAmount = new BigDecimal(receivedAmountStr);
		if (receivedAmount.compareTo(BigDecimal.ZERO) <= 0) {
			return ResponseUtil.badArgument();
		}
		LitemallFormPurchaseItem formPurchaseItem = purchaseService.queryByPurchaseIdAndItemId(formPurchaseId, id);
		if (formPurchaseItem == null) {
			return ResponseUtil.badArgument();
		}
		if(!formPurchaseItem.getCheckStatus().equals("0")){
			return ResponseUtil.badStateArgument();
		}
		formPurchaseItem.setReceivedAmount(receivedAmount);
		purchaseService.update(formPurchaseItem);

		return ResponseUtil.ok();
	}
}